$.bsgrid.forcePushPropertyInObject(
		$.fn.bsgrid.defaults.extend.renderPerRowMethods,
		'extend_render_per_row', function(record, rowIndex, trObj, options) {
			if (record != undefined && record != '') {
				trObj.attr('staffId', record.id);
			}
		});
$(function() {
	var token=localStorage.getItem("token");
	$("body").data("token",token);
	gridObj = $.fn.bsgrid.init('g_tabYeh', {
		ajaxType : 'post',
		url : server_url+'rest/getServiceorList.do?token='+token+'&ran='+Math.random(),
		autoLoad : true,
		pageSizeSelect : true,
		stripeRows : true,
		pageSize : 10,
		displayBlankRows : false,
		isProcessLockScreen : true,
		rowSelectedColor : false
	});
});
function operate(record, rowIndex, colIndex, options) {
	return '<a href="javascript:giveKey(\''
			+ gridObj.getRecordIndexValue(record, 'staffId')
			+ '\',\''+gridObj.getRecordIndexValue(record, 'staffName')+'\',\''+gridObj.getRecordIndexValue(record, 'mobile')+'\');">分配邀请码</a>'+
			'<a href="javascript:selectKey(\''
			+ gridObj.getRecordIndexValue(record, 'staffId')+ '\',\''+gridObj.getRecordIndexValue(record, 'staffName')
			+ '\');">查看可用</a>';
}
// 数据表格参数查询
function doSearch() {
	searchParames = $('#myform').serialize();
	if ($('#staff_name').val() == '') {
		gridObj.search('');
	} else {
		gridObj.search(searchParames);
	}
	console.log(searchParames);
}
//分配邀请码
function giveKey(staffId,staffName,mobile){
	var b = window.confirm("之前所分配的邀请码将会失效，确定分配吗？");
	if(b){
		var userid= localStorage.getItem("userid");
		$.ajax({
	        type : "get",
	        url :server_url+'rest/getPrivateKey.do?token='+token+'&ran=' + Math.random(),
	        data : {staffId:staffId,staffName:staffName,staffMobile:mobile,sum:10},
	        success : function(data) {
	        	if(data.retCode == "0000"){
	        		showKeys(staffName,data.data);
	        	}else{
	       	 		alert(data.retMsg);
	       	 	}
	        }
		});
	}
	
}
//查看邀请码
var selectKey = function(staffId,staffName){
	$.ajax({
        type : "get",
        url :server_url+'rest/selectPrivateKey.do?token='+token+'&ran=' + Math.random(),
        data : {staffId:staffId},
        success : function(data) {
        	if(data.retCode == "0000"){
        		showKeys(staffName,data.data);
        	}else{
       	 		alert(data.retMsg);
       	 	}
        }
	});
}
function showKeys(staffName,data){
	$(".smallBox").css('display','block');
	var s = "所属业务员:"+staffName+"\r\n";
	for(var i=0;i<data.length;i++){
		if(i ==0){
			s += data[i];
		}else{
			s +="\r\n"+data[i];
		}
	}
	$("#contentKeys").html(s);
}
function closeKeyDia(){
	$("#contentKeys").html("");
	$(".smallBox").css('display','none');
}